package cn.santi.service.impl;

import cn.santi.domain.Administrator;
import cn.santi.domain.User;
import cn.santi.mapper.AdminMapper;
import cn.santi.service.AdminService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper am;
    @Override
    public int adminAddadmin(Administrator administrator) {
        return am.insert(administrator);
    }

    @Override
    public Administrator getAdminByAccountNumber(String accountNumber) {
        LambdaQueryWrapper<Administrator> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Administrator::getAccountNumber,accountNumber);
        return am.selectOne(wrapper);
    }

    @Override
    public List<Administrator> adminGetAdminList() {
        LambdaQueryWrapper<Administrator> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderBy(true,true,Administrator::getId);
        return am.selectList(wrapper);
    }

    @Override
    public Administrator adminLogin(String name, String pwd) {
        LambdaQueryWrapper<Administrator> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Administrator::getAccountNumber,name).eq(Administrator::getPassword,pwd);
        return am.selectOne(wrapper);
    }

    @Override
    public void adminUpdateAdmin(Administrator administrator) {
        LambdaQueryWrapper<Administrator> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Administrator::getId,administrator.getId());
        am.update(administrator,wrapper);
    }

    @Override
    public Administrator getAdminById(int id) {
        return am.selectById(id);
    }

    @Override
    public Administrator adminGetUserByPwd(int idAsInt, String pwd) {
        LambdaQueryWrapper<Administrator> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Administrator::getId,idAsInt).eq(Administrator::getPassword,pwd);
        return am.selectOne(wrapper);
    }

    @Override
    public int adminUpdateAdminPwd(Administrator administrator) {
        LambdaQueryWrapper<Administrator> Wrapper = new LambdaQueryWrapper<>();
        Wrapper.eq(Administrator::getId,administrator.getId());
        return am.update(administrator,Wrapper);
    }

    @Override
    public int adminDeleteAdmin(int id) {
        return am.deleteById(id);
    }

    @Override
    public List<Administrator> adminGetAdminListByClassify(String classify) {
        LambdaQueryWrapper<Administrator> wrapper = new LambdaQueryWrapper<>();
        if (classify.equals("全部")){
            wrapper.orderBy(true,true,Administrator::getId);
        } else if (classify.equals("超管")){
            wrapper.eq(Administrator::getPosition,1).orderBy(true,true,Administrator::getId);;
        }else if (classify.equals("普通")){
            wrapper.eq(Administrator::getPosition,0).orderBy(true,true,Administrator::getId);;
        }else if (classify.equals("正常")){
            wrapper.eq(Administrator::getState,0).orderBy(true,true,Administrator::getId);;
        }else if (classify.equals("停用")){
            wrapper.eq(Administrator::getState,1).orderBy(true,true,Administrator::getId);;
        }
        return am.selectList(wrapper);
    }

}
